package com.starblink.message.messagelikeorcomment.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.util.ImageUtils
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.android.basic.util.post.PostUtils
import com.starblink.message.databinding.ItemMessageLikeOrCommentBinding
import com.starblink.rocketreserver.FetchUserMessagesV2Query
import com.starblink.basic.style.R
import com.starblink.basic.util.CommUtils
import com.starblink.message.Constants.MessageType.LIKE_COMMENT
import com.starblink.message.Constants.MessageType.LIKE_CONTENT
import com.starblink.message.Constants.MessageType.REPLY_COMMENT
import com.starblink.message.Constants.MessageType.REPLY_CONTENT

/**
 * 喜欢消息或评论消息列表项视图
 * @note 参考Flutter：MyMessageItemWidget
 */
class MessageLikeOrCommentAdapter :
    BaseQuickAdapter<FetchUserMessagesV2Query.FetchUserMessagesV2, MessageLikeOrCommentAdapter.VH>() {

    class VH(var binding: ItemMessageLikeOrCommentBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        val binding = ItemMessageLikeOrCommentBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return VH(binding)
    }

    override fun onBindViewHolder(
        holder: VH,
        position: Int,
        item: FetchUserMessagesV2Query.FetchUserMessagesV2?
    ) {
        item?.apply {
            ImageUtils.loadImage(
                context,
                friend.headImage ?: "",
                holder.binding.ivUserHead,
                ImageUtils.placeholderAvatarImage,
                ImageUtils.placeholderAvatarImage
            )

            holder.binding.tvUserName.text =
                if (friend.deleted == true) context.getString(R.string.account_deleted)
                else friend.userName ?: friend.username ?: context.getString(R.string.unknown)
            holder.binding.tvUserName.setTextColor(CommUtils.getColor(if (friend.deleted == true) R.color.sk_gray400 else R.color.sk_blue800))

            var content = ""
            if (messageType == LIKE_COMMENT || messageType == LIKE_CONTENT) {
                // Like消息
                content = "Liked your ${if (messageType == LIKE_COMMENT) "comment" else "note"} · ${
                    PostUtils.getTimeDiffDetailFriendly(sendTime as String)
                }"
            } else if (messageType == REPLY_COMMENT || messageType == REPLY_CONTENT) {
                // Comment消息
                content =
                    "${if (messageType == REPLY_CONTENT) "comment to your content · " else "replies to your comment · "} ${
                        PostUtils.getTimeDiffDetailFriendly(sendTime as String)
                    }"
            }
            holder.binding.tvContent.text = content

            // 加载发布主图
            ImageUtils.loadImage(
                context,
                if (messageType == LIKE_CONTENT)
                    post?.postF?.mainPic ?: "" else postComment?.post?.postF?.mainPic
                    ?: "", // 仅点赞内容和评论无关
                holder.binding.ivPostMainPic,
                ImageUtils.errorImage2,
                ImageUtils.errorImage2
            )
            // 如果发布内容是2，显示视频图标
            holder.binding.ivVideoFlag.visibleOrGone(postComment?.post?.postF?.contentType == "2")

            // 设置回复的内容
            // messageType=LIKE_CONTENT时不显示
            holder.binding.layoutComment.visibleOrGone(
                messageType == LIKE_COMMENT ||
                        messageType == REPLY_COMMENT || messageType == REPLY_CONTENT
            ) {
                    if (postComment?.deleted == true || friend.deleted == true){
                        // 如果评论已删除或评论所属用户已删除
                        holder.binding.tvComment.text = "The comment has since been deleted."
                        holder.binding.tvComment.setTextColor(CommUtils.getColor(R.color.sk_gray300))
                    } else{
                        holder.binding.tvComment.text = postComment?.content
                        holder.binding.tvComment.setTextColor(CommUtils.getColor(R.color.sk_gray800))
                    }
            }

            // 评论和点赞
            var visible: Boolean
            if (messageType == REPLY_COMMENT || messageType == REPLY_CONTENT) {
                var deleted: Boolean
                var accountDeleted = friend.deleted ?: false

                deleted = if (messageType == REPLY_COMMENT) {
                    postComment?.deleted ?: false
                } else {
                    postComment?.deleted ?: true
                }
                if (postComment?.post?.postF?.deleted == true) {
                    deleted = true
                }
                visible = deleted == false && accountDeleted == false
            } else {
                visible = false
            }
            holder.binding.groupReplyAndLike.visibleOrGone(visible) {
                setLikeState(postComment?.liked ?: false, holder.binding)
            }
            holder.binding.layoutLike.setOnClickListener {
                if(postComment == null){
                    return@setOnClickListener
                }
                CommonMutateManage.likePostComment(
                    context,
                    postComment!!.id,
                    postComment!!.liked,
                    0,
                    {
                        // 刷新状态
                        setLikeState(!postComment!!.liked, holder.binding)
                    })
                { like, likeCount ->
                    ApolloValSetExt.setApolloVal<FetchUserMessagesV2Query.PostComment>(
                        postComment!!,
                        "liked",
                        like
                    )
                    setLikeState(like, holder.binding)
                }
            }
        }
    }
    private fun setLikeState(like: Boolean, binding: ItemMessageLikeOrCommentBinding) {
        binding.ivLikeComment.setImageResource(if (like) R.mipmap.video_like else R.mipmap.unlike)
    }
}